﻿Imports CoreCode
Imports CoreCode.Connection

Public Class frmLogin

    Public mode As eMode
    Public action As eAction
    Public occupied As Boolean
    Public DSet As New DataSet

    Dim clsDBCon As New Connection
    Dim sqlAdapter As New System.Data.SqlClient.SqlDataAdapter
    Dim a As New BindingSource

    Dim sTableName As String

    Dim lPush As Boolean

    Private Sub frmLogin_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Try
            clsDBCon = Nothing
        Catch ex As Exception
            Me.Close()
        End Try
    End Sub

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ClearData
    End Sub

    Public Function TBConnect() As Boolean
        With clsDBCon
            .ObjectControl(Me, ObjectMethod.ObjectLock, True)
            .DatabasePassword = My.Settings.ConnectionPassword
            .DatabaseServer = My.Settings.ConnectionServer
            .DatabaseServerType = ServerType.MSSQL
            .DatabaseUserName = My.Settings.ConnectionUserName
            
            .DatabaseName = My.Settings.SecDatabaseName
            Return .Connect(.SQLCn)

        End With
    End Function

    Private Sub txtPassword_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPassword.LostFocus
        Try
            With clsDBCon
                sTableName = "sec_user"
                mode = eMode.Normal
                .WaitWindow(WaitLoginMsg, True)
                If TBConnect() = True Then
                    .OpenRecord(.SQLCn, sqlAdapter, DSet, "*", sTableName, "cUserName = '" & txtUserName.Text & "'")
                    a.DataSource = DSet.Tables(0)
                    If a.Count > 0 Then
                        If txtPassword.Text <> DSet.Tables(0).Rows(0).Item("cPassword") Then
                            .WaitWindow("", False)
                            MsgBox("Password is incorrect", MsgBoxStyle.Critical, Connection.TitleHeader)
                            .ObjectControl(Me, ObjectMethod.ObjectLock, False)
                            txtPassword.Focus()
                        Else
                            .WaitWindow("", False)
                            .LoadComboValues(cboCompany, "b.cCompanyID", sTableName & "_company a left outer join sec_company b on a.cCompanyID = b.nID", "a.cUserName='" & txtUserName.Text & "'")
                            .ObjectControl(Me, ObjectMethod.ObjectLock, True)
                        End If
                    Else
                        .WaitWindow("", False)
                        MsgBox("User not found", MsgBoxStyle.Critical, Connection.TitleHeader)
                        .ObjectControl(Me, ObjectMethod.ObjectLock, False)
                        txtUserName.Focus()
                    End If
                Else
                    .WaitWindow("", False)
                    .ObjectControl(Me, ObjectMethod.ObjectLock, False)
                End If
            End With
        Catch ex As Exception
            clsDBCon.WaitWindow("", False)
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Try
            TBConnect()
            clsDBCon.WaitWindow(WaitLoginMsg, True)
            If txtUserName.Text = "" Then
                MsgBox("Username cannot be empty", MsgBoxStyle.Critical, Connection.TitleHeader)
                Exit Sub
            End If
            If cboCompany.Text = "" Then
                MsgBox("Company ID cannot be empty", MsgBoxStyle.Critical, Connection.TitleHeader)
                Exit Sub
            End If
            If cboRole.Text = "" Then
                MsgBox("Role ID cannot be empty", MsgBoxStyle.Critical, Connection.TitleHeader)
                Exit Sub
            End If

            'CheckDatabaseExistance
            With My.Settings
                clsDBCon.CheckDBExistance(clsDBCon.SQLCn, .SetDatabaseName, .BackupPath, .DataPath, .LogPath)
                clsDBCon.CheckDBExistance(clsDBCon.SQLCn, .TranDatabaseName, .BackupPath, .DataPath, .LogPath)
            End With

            With clsDBCon
                .ConnectionServer = My.Settings.ConnectionServer
                .ConnectionPassword = My.Settings.ConnectionPassword
                .ConnectionUserName = My.Settings.ConnectionUserName
                .SecDatabaseName = My.Settings.SecDatabaseName
                .SetDatabaseName = My.Settings.SetDatabaseName
                .TranDatabaseName = My.Settings.TranDatabaseName

                .DatabaseName = My.Settings.SecDatabaseName
                .Connect(.SQLCnSec)
                .DatabaseName = My.Settings.SetDatabaseName
                .Connect(.SQLCnSet)
                .DatabaseName = My.Settings.TranDatabaseName
                .Connect(.SQLCnTran)

                .CurrentUserName = txtUserName.Text
                .CurrentFullName = .GetValueFrTable("cUserFullName", sTableName, "cUserName='" & txtUserName.Text & "'")
                .CurrentCompany = cboCompany.Text
                .CurrentRole = cboRole.Text

                .WaitWindow("", False)
                .WaitWindow("", True, True)
            End With

            Dim frmMain As New ssMain
            frmMain.Show()
            Me.Hide()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub btnOption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOption.Click
        If lPush = False Then
            Me.Size = New Size(337, 310)
            lPush = True
        Else
            Me.Size = New Size(337, 333)
            lPush = False
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        My.Settings.ConnectionServer = txtServerAddress.Text
        My.Settings.ConnectionPassword = txtServerPassword.Text
        My.Settings.ConnectionUserName = txtServerUser.Text
        My.Settings.SecDatabaseName = txtSecurity.Text
        My.Settings.SetDatabaseName = txtSettings.Text
        My.Settings.TranDatabaseName = txtTransaction.Text
        My.Settings.BackupPath = txtBackupPath.Text
        My.Settings.DataPath = txtDataPath.Text
        My.Settings.LogPath = txtLogPath.Text
        My.Settings.Save()
        MsgBox("Settings successfully save!", MsgBoxStyle.Information, TitleHeader)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub cboCompany_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCompany.SelectedIndexChanged
        If cboCompany.Text = "" Then Exit Sub
        With clsDBCon
            .LoadComboValues(cboRole, "c.cRoleID", sTableName & "_company_role a left outer join sec_user_company b on a.nRefID = b.cCompanyID left outer join sec_role c on a.cRoleRefID = c.nID", "b.cUserName='" & txtUserName.Text & "'")
            lblCompany.Text = .GetValueFrTable("cCompanyName", "sec_company", "cCompanyID='" & cboCompany.Text & "'")
        End With
    End Sub

    Private Sub cboRole_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboRole.SelectedIndexChanged
        lblRole.Text = clsDBCon.GetValueFrTable("cRoleName", "sec_role", "cRoleID='" & cboRole.Text & "'")
        btnLogin.Enabled = True
        btnCancel.Enabled = True
    End Sub

    Private Sub LabelX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelX1.Click
        txtUserName.Focus()
    End Sub

    Private Sub LabelX2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelX2.Click
        txtPassword.Focus()
    End Sub

    Private Sub LabelX3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelX3.Click
        cboCompany.Focus()
    End Sub

    Private Sub LabelX4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelX4.Click
        cboRole.Focus()
    End Sub 

    Public Sub ClearData()
        txtUserName.Text = ""
        txtPassword.Text = ""
        cboCompany.Items.Clear()
        cboCompany.Text = ""
        lblCompany.Text = ""
        cboRole.Items.Clear()
        cboRole.Text = ""
        lblRole.Text = ""
        btnLogin.Enabled = True
        btnCancel.Enabled = True
        btnOption.Enabled = True
        txtUserName.ReadOnly = False
        txtPassword.ReadOnly = False
        clsDBCon.ObjectControl(Me, ObjectMethod.ObjectLock, False)


        Me.Text = My.Application.Info.Title
        txtUserName.Text = Replace(My.User.Name, My.Computer.Name & "\", "")
        Me.Size = New Size(337, 310)
        lPush = False

        txtServerAddress.Text = My.Settings.ConnectionServer
        txtServerPassword.Text = My.Settings.ConnectionPassword
        txtServerUser.Text = My.Settings.ConnectionUserName
        txtSecurity.Text = My.Settings.SecDatabaseName
        txtSettings.Text = My.Settings.SetDatabaseName
        txtTransaction.Text = My.Settings.TranDatabaseName
        txtBackupPath.Text = My.Settings.BackupPath
        txtDataPath.Text = My.Settings.DataPath
        txtLogPath.Text = My.Settings.LogPath

        txtUserName.Focus()

    End Sub

End Class
